package com.book.workbench.service.impl;

import com.book.common.UUidUtil;
import com.book.settings.mapper.UserMapper;
import com.book.settings.pojo.User;
import com.book.workbench.mapper.BookMapper;
import com.book.workbench.pojo.Book;
import com.book.workbench.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public Integer addBook(Book book) {
        Book book1 = bookMapper.getBookByNumber(book.getNumber());
        if(book1==null){
            book.setId(UUidUtil.getUUID());
            return bookMapper.addBook(book);
        }else{
            return -1;
        }
    }

    @Override
    public Integer deleteBookById(Book book) {
        int deleteBookResult = bookMapper.deleteBookById(book);
        return deleteBookResult;
    }

    @Override
    public PageInfo<Book> getListBook(Book book) {
        PageHelper.startPage(book.getPageNo(),book.getPageSize());
        List<Book> books = bookMapper.getListBook(book);
        PageInfo<Book> page = new PageInfo<>(books,book.getPageSize());
        return page;
    }

    @Override
    public Integer getCountListBook(Book book) {
        return bookMapper.getCountListBook(book);
    }

    @Override
    public Integer updateBookById(Book book) {
        int updateBookById = bookMapper.updateBookById(book);
        return updateBookById;
    }

    @Override
    public Integer updateBookExist(String book_id, String exist) {
        int updateBookExist = bookMapper.updateBookExist(book_id,exist);
        return updateBookExist;
    }

    @Override
    public Integer searchBookIsExist(String book_id) {
        int isExist = bookMapper.searchBookIsExist(book_id);
        return isExist;
    }

    @Override
    public PageInfo<Book> getListBookBy(Book book) {
        PageHelper.startPage(book.getPageNo(),book.getPageSize());
        List<Book> books = bookMapper.getListBookBy(book);
        PageInfo<Book> page = new PageInfo<>(books,book.getPageSize());
        return page;
    }

    @Override
    public Integer getCountListBookBy(Book book) {
        return bookMapper.getCountListBookBy(book);
    }

    @Override
    public User getUserByAct(String act) {
        User user1 = userMapper.getUserByAct(act);
        return user1;
    }

    @Override
    public List<Book> searchUserNotReturnBook(String user_id) {
        List<Book> bookList = bookMapper.searchUserNotReturnBook(user_id);
        return bookList;
    }

    @Override
    public PageInfo<Book> getListAllBook(Book book) {
        PageHelper.startPage(book.getPageNo(),book.getPageSize());
        List<Book> books = bookMapper.getListAllBook(book);
        PageInfo<Book> page = new PageInfo<>(books,book.getPageSize());
        return page;
    }

    @Override
    public Integer getCountListAllBook(Book book) {
        return bookMapper.getCountListAllBook(book);
    }

    @Override
    public Book searchBookById(String id) {
        return bookMapper.searchBookById(id);
    }
}
